package com.tn.utils;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ：yuxiaolong
 * @description：json工具类.参考https://blog.csdn.net/zzti_erlie/article/details/79779253
 * @date ：2020/11/12 13:41
 * @modified By：
 * @version: 1.0.0
 */
@Slf4j
public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将对象转为json String
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> String obj2String(T obj){

        if(obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String)obj :  objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.warn("Parse Object to String error",e);
            return null;
        }
    }

    /**
     * 将对象转为json，并格式化的输出
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> String obj2StringPretty(T obj){
        if(obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String)obj :  objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception e) {
            log.warn("Parse Object to String error",e);
            return null;
        }
    }

    /**
     * 将json转为java对象，方式一
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T string2Obj(String str, Class<T> clazz){
        if(StringUtils.isEmpty(str) || clazz == null){
            return null;
        }

        try {
            return clazz.equals(String.class)? (T)str : objectMapper.readValue(str, clazz);
        } catch (Exception e) {
            log.warn("Parse String to Object error",e);
            return null;
        }
    }

//    /**
//     * 将json转为java对象，方式二
//     * @param str
//     * @param typeReference
//     * @param <T>
//     * @return
//     */
//    public static <T> T string2Obj(String str, TypeReference<T> typeReference){
//        if(StringUtils.isEmpty(str) || typeReference == null){
//            return null;
//        }
//        try {
//            return (T)(typeReference.getType().equals(String.class)? str : objectMapper.readValue(str,typeReference));
//        } catch (Exception e) {
//            log.warn("Parse String to Object error",e);
//            return null;
//        }
//    }

    /**
     * 将json转为java对象，方式s三
     * @param str
     * @param collectionClass
     * @param elementClasses
     * @param <T>
     * @return
     */
    public static <T> T string2Obj(String str, Class<?> collectionClass, Class<?>... elementClasses){
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
        try {
            return objectMapper.readValue(str,javaType);
        } catch (Exception e) {
            log.warn("Parse String to Object error",e);
            return null;
        }
    }
}
